/**********************************************************************************/
/* bintoc - bintoc.cpp                                                            */
/*                                                                                */
/* Programmed by Kevin Pickell                                                    */
/*                                                                                */
/* http://code.google.com/p/kgui/	                                              */
/*                                                                                */
/*    kGUI is free software; you can redistribute it and/or modify                */
/*    it under the terms of the GNU Lesser General Public License as published by */
/*    the Free Software Foundation; version 2.                                    */
/*                                                                                */
/*    kGUI is distributed in the hope that it will be useful,                     */
/*    but WITHOUT ANY WARRANTY; without even the implied warranty of              */
/*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               */
/*    GNU General Public License for more details.                                */
/*                                                                                */
/*    http://www.gnu.org/licenses/lgpl.txt                                        */
/*                                                                                */
/*    You should have received a copy of the GNU General Public License           */
/*    along with kGUI; if not, write to the Free Software                         */
/*    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  */
/*                                                                                */
/**********************************************************************************/

/*! @file bintoc.cpp 
    @brief Bintoc is a stand alone console app that converts a binary data file to
	a c file so it can be included directly into an application */

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <sys/stat.h>

char *loadfilez(char *filename,int *filesize)
{
	FILE *fp;
	char *filemem;
	int count,fs;
	long /*fpos_t*/ fsize;

	if(filesize)
		*(filesize)=0;	/* if there is any errors, then filesize=0 */
	
	fp=fopen(filename,"rb");
	
	if(!fp)			/* error! */
	{
		printf("Error: (OPEN) Unable to open input file '%s'\n",filename);
		return(0);
	}
	if (fseek (fp,0l,SEEK_END))
	{
		printf("Error: (SEEK) Unable to open input file '%s'",filename);
		return (0);
	}
	fsize=ftell(fp);
	fs=(int)fsize;
	filemem=(char *)malloc(fs);	/* let's allocate the ram to load the file in! */
	if(!filemem)
	{
		printf("Error: (ALLOC) Unable allocate memory '%d'",fs);
		fclose(fp);
		return(0);		/* not enough ram to load the file in! */
	}
	if (fseek (fp,0l,SEEK_SET))
	{
		printf("Error: (SEEK) Unable to open input file '%s'",filename);
		return (0);
	}
	count = (int)fread (filemem,1,fs,fp);	/* read in the file */
	fclose(fp);
	if(fs!=count)
	{
		free(filemem);
		printf("Error: (COUNT MISMATCH) Unable to open input file '%s',%d!=%d",filename,count,fs);
		return(0);
	}		

	/* everything was ok! */
	if(filesize)
		*(filesize)=fs;
	return(filemem);
}

long MS_FileTime(const char *fn)
{
#if defined(LINUX) || defined(MINGW) || defined(MACINTOSH)
	int result;
	struct stat buf;

	result=stat(fn,&buf);
	if(result==-1)
		return(0);
	return(buf.st_mtime);
#else
   struct __stat64 buf;
   int result;

   result = _stat64( fn, &buf );

   /* Check if statistics are valid: */
   if( result != 0 )
		return(0);
   else
		return((long)buf.st_mtime);
#endif
}

int main(int argc, char *argv[])
{
	char *inname;
	char *outname;
	FILE *fout;
	int i;

	char *indata;
	int insize;

	if(argc<3)
	{	
		printf("usage: bintoc inname outname");
		return(0);
	}
	inname=argv[1];
	outname=argv[2];

	/* if output file exists already and is older than the input file then */
	/* just return as it is already up to date */
	{
		long intime;
		long outtime;
		
		intime=MS_FileTime(inname);
		outtime=MS_FileTime(outname);
		if((intime<=outtime) && (outtime!=0))
		{
			printf("output file is newer so no need to generate it!\n");
			return(0);
		}
	}

	indata=loadfilez(inname,&insize);
	if(!indata)
	{
		printf("Error opening file '%s'\n",inname);
		return(0);
	}
	fout=fopen(outname,"w");
	if(!fout)
	{
		printf("Error opening output '%s'\n",outname);
		return(0);
	}

	fprintf(fout,"/* file generated by bintoc, by Kevin Pickell, 2004 */\n");
	fprintf(fout,"unsigned char bin_");
	i=0;
	do
	{
		fprintf(fout,"%c",inname[i++]);
	}while(inname[i] && inname[i]!='.');
	fprintf(fout,"[%d]={",insize);
	i=0;
	for(i=0;i<(insize-1);)
	{
		fprintf(fout,"%d,",indata[i]&255);
		++i;
		if(!(i&31))
			fprintf(fout,"\n");
	}
	fprintf(fout,"%d};\n",indata[i]);
	fclose(fout);
	free(indata);
	return 0;
}

